package com.cxd.app.controller;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.alibaba.fastjson.JSONObject;
import com.cxd.app.entity.base.UserTokenBase;
import com.cxd.app.service.UserService;
import com.cxd.app.service.WdtyService;
import com.cxd.base.common.exception.CxdException;
import com.cxd.base.controller.BaseController;
import com.cxd.base.util.BeanTools;

/**
 * 网贷天眼
 * @author wlsnow
 */
@Controller("WdtyController")
@RequestMapping("")
public class WdtyController extends BaseController{
	private static final Logger logger = Logger.getLogger(WdtyController.class);
	
	@Resource
	public WdtyService wdtyService;
	
	@Resource
	public UserService userService;
	
	@Resource
	public BeanTools bt;
	
	/**
	 * 网贷天眼获取借款信息
	 * @author wlsnow
	 *  status 		Int 	Yes 	标的状态:0.正在投标中的借款标;1.已完成(包括还款中和已完成的借款标).
		time_from 	String 	Yes 	起始时间如:2014-05-09 06:10:00, 状态为1是对应平台满标字段的值检索,状态为0就以平台发标时间字段检索.
		time_to 	String 	Yes 	截止时间如:2014-05-09 06:10:00,状态为1是对应平台满标字段的值检索,状态为0就以平台发标时间字段检索.
		page_size 	Int 	Yes 	每页记录条数.
		page_index 	Int 	Yes 	请求的页码.
		token 		String 	No 		请求 token 链接平台返回的秘钥或签名.
	 */
	@RequestMapping(value = "wdty_loans", method = {RequestMethod.GET,RequestMethod.POST })
	public void wdty_loans(HttpServletResponse response,String status,String time_from,String time_to,String page_size,String page_index,String token)throws IOException {
		super.requestLog("wdty_loans",String.format("状态:%s,时间:%s--%s,页大小:%s,当前页:%s",status,time_from,time_to,page_size,page_index));
		JSONObject json = new JSONObject();
		Map<String, Object> param = new HashMap<String,Object>();
		param.put("status", status.toString().trim());
		param.put("time_from", time_from);
		param.put("time_to", time_to);
		param.put("pageSize", page_size);
		param.put("currentPage", page_index);
		try {
			UserTokenBase utBean = bt.queryBean("USER_TOKEN", "token", token, UserTokenBase.class);
			if(utBean != null && (utBean.getCtime().getTime()+3600*1000)>new Date().getTime()){
				wdtyService.wdty_loans(json, param);
			}else{
				json.put("msg", "token错误,请重新登录");
			}
		} catch(CxdException e){
			super.cxdJsonException(json, e,String.format("状态:%s,时间:%s--%s,页大小:%s,当前页:%s",status,time_from,time_to,page_size,page_index));
		}catch (Exception e) {
			super.jsonException(json, e, logger);
		}
		super.responseSendMsg(response, json.toJSONString().replace("null", "''"));
	}
	
	/**
	 * 网贷天眼获取投资信息
	 * @author wtj
	 * @date 2016年5月4日下午3:57:16
	 * @param response
	 * @param id
	 * @param page_size
	 * @param page_index
	 * @param token
	 * @throws IOException
	 */
	@RequestMapping(value = "wdty_invests", method = {RequestMethod.GET,RequestMethod.POST })
	public void wdty_invests(HttpServletResponse response,String id,String page_size,String page_index,String token)throws IOException {
		String logInfo = String.format("借款ID:%s,页大小:%s,当前页:%s",id,page_size,page_index);
		super.requestLog("wdty_invests",logInfo);
		JSONObject json = new JSONObject();
		Map<String, Object> param = new HashMap<String,Object>();
		param.put("pageSize", page_size);
		param.put("currentPage", page_index);
		param.put("loanId", id);
		try {
			UserTokenBase utBean = bt.queryBean("USER_TOKEN", "token", token, UserTokenBase.class);
			if(utBean != null && (utBean.getCtime().getTime()+3600*1000)>new Date().getTime()){
				wdtyService.wdty_invests(json, param);
			}else{
				json.put("msg", "token错误,请重新登录");
			}
		} catch(CxdException e){
			super.cxdJsonException(json, e,logInfo);
		}catch (Exception e) {
			super.jsonException(json, e, logger);
		}
		super.responseSendMsg(response, json.toJSONString().replace("null", "''"));
	}
	/**
	 * 网贷天眼登录
	 * @author wtj
	 * @date 2016年5月4日下午3:53:44
	 * @param response
	 * @param username
	 * @param password
	 * @throws IOException
	 */
	@RequestMapping(value = "wdty_login", method = {RequestMethod.GET,RequestMethod.POST })
	public void wdty_login(HttpServletResponse response,String username,String password)throws IOException {
		String logInfo = String.format("用户名:%s,密码:%s",username,password);
		super.requestLog("wdty_login",logInfo);
		JSONObject json = new JSONObject();
		try {
			userService.toLogin(json, username, password);
			Map<String, Object> param = new HashMap<String,Object>();
			if(json.getString("status").equals("200")){
				param.put("token", json.getObject("result", Map.class).get("token"));
				json.put("result", "1");
			}else{
				param.put("token","");
				json.put("result", "-1");
			}
			json.put("data", param);
			json.remove("status");
			json.remove("msg");
		} catch(CxdException e){
			super.cxdJsonException(json, e,logInfo);
		}catch (Exception e) {
			super.jsonException(json, e, logger);
		}
		super.responseSendMsg(response, json.toJSONString().replace("null", "''"));
	}
}
